package com.zzptc.gyc.wodelianxi;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;

import android.widget.Switch;
import android.widget.Toast;

import com.zzptc.gyc.wodelianxi.entity.User;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {
EditText et_gyc_name,et_gyc_pwd;
Switch aSwitch;
Button btn_gyc_login;
boolean flag;
    private List<User> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//实例化控件
        eventListener();
        User user= new User("admin","123");
        User user1= new User("aaa","123");
        User user2 = new User("bbb","123");
        list.add(user);
        list.add(user1);
        list.add(user2);
    }

    private void eventListener() {
        //自动登录是否被改变
        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 if(isChecked){
     flag=true;
 }else{
     flag=false;
 }
                Toast.makeText(MainActivity.this,""+flag,Toast.LENGTH_SHORT).show();
            }
        });
        btn_gyc_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(flag){
                    //获取用户名和密码的值
                    String name = et_gyc_name.getText().toString().trim();
                    String pwd = et_gyc_pwd.getText().toString().trim();
                    SharedPreferences sh= getSharedPreferences ("one",MODE_PRIVATE);
                    SharedPreferences.Editor edit =sh.edit();
                    //记住(用户名/密码/是否自动登陆)
         if(flag){
             edit.putString("uname",name);
             edit.putString("password",pwd);
             edit.putBoolean("autologin",flag);
             edit.commit();
         }else {
             edit.putString("uname","");
             edit.putString("password","");
             edit.putBoolean("autologin",flag);
             edit.commit();
         }
         //登陆
                    login(name,pwd);
                }
            }
        });
    }

  public  void login(String usename,String passpwd){
//循环
      boolean isLogin=false;
      for (User u:list){
          if(u.getUsername().equals(usename)&&u.getPassword().equals(passpwd)){
              //找到该用户了
              Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
              isLogin= true;
              break;
          }else {
              isLogin=false;

          }

      }
      if(isLogin){
          Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
      }else{
          Toast.makeText(this,"登陆失败",Toast.LENGTH_SHORT).show();
      }
  }
    private void init() {
        et_gyc_name=findViewById(R.id.et_gyc_name);
        et_gyc_pwd=findViewById(R.id.et_gyc_pwd);
        aSwitch=findViewById(R.id.switch_auto_login);
        btn_gyc_login=findViewById(R.id.btn_gyc_login);

    }
}
